/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  cn.hutool.core.util.StrUtil
 *  javafx.stage.FileChooser
 *  javafx.stage.FileChooser$ExtensionFilter
 *  javafx.stage.Stage
 *  javafx.stage.Window
 *  org.apache.poi.hssf.usermodel.HSSFCell
 *  org.apache.poi.hssf.usermodel.HSSFCellStyle
 *  org.apache.poi.hssf.usermodel.HSSFFont
 *  org.apache.poi.hssf.usermodel.HSSFRichTextString
 *  org.apache.poi.hssf.usermodel.HSSFRow
 *  org.apache.poi.hssf.usermodel.HSSFSheet
 *  org.apache.poi.hssf.usermodel.HSSFWorkbook
 *  org.apache.poi.ss.usermodel.RichTextString
 */
package com.gys.common.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.gys.common.util.CommonUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.RichTextString;

public class ExcelUtil {
    private static HSSFWorkbook workbook;

    public static <T> boolean export(String fileName, List<T> records, List<String> columnName, List<String> columnId) {
        Stage stage = new Stage();
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("\u5bfc\u51fa");
        fileChooser.setInitialFileName(fileName);
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XLS Files", new String[]{"*.xls"}));
        File file = fileChooser.showSaveDialog((Window)stage);
        if (file != null) {
            ExcelUtil.exportExcel(file.getAbsolutePath(), records, columnName, columnId);
            return true;
        }
        return false;
    }

    private static <T> void exportExcel(String fileName, List<T> records, List<String> columnName, List<String> columnId) {
        int i;
        workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("LaundryService\u8bb0\u5f55");
        sheet.setDefaultColumnWidth((short)15);
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor((short)40);
        style.setFillPattern((short)1);
        style.setBorderBottom((short)1);
        style.setBorderLeft((short)1);
        style.setBorderRight((short)1);
        style.setBorderTop((short)1);
        style.setAlignment((short)2);
        HSSFFont font = workbook.createFont();
        font.setColor((short)20);
        font.setFontHeightInPoints((short)12);
        font.setBoldweight((short)700);
        style.setFont(font);
        HSSFRow row = sheet.createRow(0);
        for (i = 0; i < columnName.size(); ++i) {
            HSSFCell cell1 = row.createCell(i);
            cell1.setCellType(1);
            cell1.setCellValue((RichTextString)new HSSFRichTextString(columnName.get(i)));
        }
        for (i = 0; i < records.size(); ++i) {
            HSSFRow datarow = sheet.createRow(i + 1);
            T object = records.get(i);
            for (int j = 0; j < columnId.size(); ++j) {
                String methodName = CommonUtil.getGetMethod(columnId.get(j));
                String value = "";
                try {
                    Method method = object.getClass().getMethod(methodName, new Class[0]);
                    Object result = method.invoke(object, new Object[0]);
                    if (ObjectUtil.isNotNull(result)) {
                        value = StrUtil.toString(result);
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                HSSFCell datacell1 = datarow.createCell(j);
                datacell1.setCellType(1);
                datacell1.setCellValue(value);
            }
        }
        ExcelUtil.outputExcel(fileName);
    }

    public static void outputExcel(String fileName) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(fileName));
            workbook.write((OutputStream)fos);
            fos.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

